# 计算1～100000之间所有素数的个数， 要求如下:
# - 编写函数判断一个数字是否为素数，然后统计素数的个数；
# -对比1: 对比使用多进程和不使用多进程两种方法的统计速度。
# -对比2：对比开启4个多进程和开启10个多进程两种方法的速度。
import threading
import time

def su(n):
    Flag = True
    for i in range(2, n//2):
        if n % i == 0:
            Flag = False
    return Flag


sum = 0
i = 0

start_time = time.time()
for i in range(1,100000):
   if su(i):
       sum = sum + 1
print(sum)
end_time = time.time()
print('程序用时：%s秒' % int(end_time - start_time))


def fun3():
    global sum
    global i
    for i in range(i, 100000):
        if su(i):
            mutex.acquire()
            sum = sum + 1
            mutex.release()
    print(sum)


mutex = threading.Lock()

if __name__ == "__main__":
    from concurrent.futures import ThreadPoolExecutor

    threadPool = ThreadPoolExecutor(max_workers=10)
    for i in range(0, 10):
        future = threadPool.submit(fun3())
    threadPool.shutdown(wait=True)


